package com.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.javabean.*;

import com.model.MeetingService;
import com.model.RequestAttribute;
/**
 * Servlet implementation class MeetingRoomDeal
 */
@WebServlet("/files/MeetingRoomDeal")
public class MeetingRoomDeal extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MeetingRoomDeal() {
        super();
    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		MeetingService ms=new MeetingService();//提供服务的model
		String flag=request.getParameter("flag");
		if(flag.equals("book")){
		      /*需要接受的参数
					String ReserveDate, int RoomID, int OccupyTime,
					int AttenderNumber, String ReserverName, String ConnectorName,
					String ConnectMeans, String Description*/
			String ReserveDate=request.getParameter("bookingTime");
			String SRoomID=request.getParameter("RoomID");
			int RoomID=0;
			if(SRoomID.equals("B2-617")){RoomID=1;}else if(SRoomID.equals("B2-708")){RoomID=2;}else if(SRoomID.equals("B2-810")){RoomID=3;}else{RoomID=4;};
			String SOccupyTime=request.getParameter("OccupyTime");
			int OccupyTime=0;
			if(SOccupyTime.equals("8:30-10:00")){OccupyTime=1;}else if(SOccupyTime.equals("10:30-12:00")){OccupyTime=2;}
			else if(SOccupyTime.equals("14:30-16:00")){OccupyTime=3;}else{OccupyTime=4;};
			String SAttenderNumber=request.getParameter("AttenderNumber");
			int AttenderNumber=0;
			if(SAttenderNumber.equals("20人以下")){AttenderNumber=1;}else if(SAttenderNumber.equals("20-30人")){AttenderNumber=2;}else{AttenderNumber=3;};
			String ReserverName=request.getParameter("ReserverName");
			String ConnectorName=request.getParameter("ConnectorName");
			String ConnectMeans=request.getParameter("ConnectMeans");
			String Description=request.getParameter("Description");
			String Department=request.getParameter("Department");
			int UserID=(int) ((User)request.getSession().getAttribute("user")).getUserId();
			if(ms.ReserveRoom(ReserveDate, RoomID, OccupyTime, AttenderNumber, ReserverName, ConnectorName, ConnectMeans, Description, Department,UserID)){
				//System.out.println(ConnectorName+Description);
				 RequestAttribute.setConName(request,ConnectorName);
				 RequestAttribute.setConMeans(request,ConnectMeans);
				 RequestAttribute.setDescription(request,Description);
				 RequestAttribute.setDate(request,ReserveDate);
				 request.getRequestDispatcher("/files/meetBooking.jsp").forward(request,response);//成功跳转的页面
			}else{
				response.sendRedirect("ErrorDeal?error=MD");
				//失败跳转的页面
			}

		}else if(flag.equals("agreeReserve")){
			//int state,int ReserveID
			String[] delLpns;
			delLpns = request.getParameterValues("delid");
			
			if( delLpns==null ) {
				response.sendRedirect("meetingRoom.jsp");
				return;
			}
			for(int i=0; i<delLpns.length; i++) {
				int ReserveID=Integer.parseInt(delLpns[i]);
				ms.changeRoomState(3, ReserveID);
				
			}
			response.sendRedirect("meetingRoom.jsp");
			return;
		}else if(flag.equals("disagreeReserve")){
			//int state,int ReserveID
			String[] delLpns;
			delLpns = request.getParameterValues("delid");
			
			if( delLpns==null ) {
				response.sendRedirect("meetingRoom.jsp");
				return;
			}
			for(int i=0; i<delLpns.length; i++) {
				int ReserveID=Integer.parseInt(delLpns[i]);
				ms.changeRoomState(4, ReserveID);
			}
			response.sendRedirect("meetingRoom.jsp");
			return;
		}else if(flag.equals("deleteReserve")){
			//int state,int ReserveID
			String[] delLpns;
			delLpns = request.getParameterValues("delid");
			
			if( delLpns==null ) {
				response.sendRedirect("meetingRoom.jsp");
				return;
			}
			for(int i=0; i<delLpns.length; i++) {
				int ReserveID=Integer.parseInt(delLpns[i]);
				ms.deleteRoom(ReserveID);
			}
			response.sendRedirect("meetingRoom.jsp");
			return;
		}else if(flag.equals("agree")){
			int ReserveID=Integer.parseInt(request.getParameter("ReserveID"));
			ms.changeRoomState(3, ReserveID);
			return;
		}else if(flag.equals("disagree")){
			int ReserveID=Integer.parseInt(request.getParameter("ReserveID"));
			ms.changeRoomState(4, ReserveID);
			return;
			//外部预约
		}else if(flag.equals("outBook")){
		      /*需要接受的参数
					String ReserveDate, int RoomID, int OccupyTime,
					int AttenderNumber, String ReserverName, String ConnectorName,
					String ConnectMeans, String Description*/
			String ReserveDate=request.getParameter("bookingTime");
			String SRoomID=request.getParameter("RoomID");
			int RoomID=0;
			if(SRoomID.equals("B2-617")){RoomID=1;}else if(SRoomID.equals("B2-708")){RoomID=2;}else if(SRoomID.equals("B2-810")){RoomID=3;}else{RoomID=4;};
			String SOccupyTime=request.getParameter("OccupyTime");
			int OccupyTime=0;
			if(SOccupyTime.equals("8:30-10:00")){OccupyTime=1;}else if(SOccupyTime.equals("10:30-12:00")){OccupyTime=2;}
			else if(SOccupyTime.equals("14:30-16:00")){OccupyTime=3;}else{OccupyTime=4;};
			String SAttenderNumber=request.getParameter("AttenderNumber");
			int AttenderNumber=0;
			if(SAttenderNumber.equals("20人以下")){AttenderNumber=1;}else if(SAttenderNumber.equals("20-30人")){AttenderNumber=2;}else{AttenderNumber=3;};
			String ReserverName=request.getParameter("ReserverName");
			String ConnectorName=request.getParameter("ConnectorName");
			String ConnectMeans=request.getParameter("ConnectMeans");
			String Description=request.getParameter("Description");
			String Department=request.getParameter("Department");
			int UserID=(int) ((User)request.getSession().getAttribute("user")).getUserId();
			if(!ms.checkRoom(ReserveDate, RoomID, OccupyTime)){
				 RequestAttribute.setConName(request,ConnectorName);
				 RequestAttribute.setConMeans(request,ConnectMeans);
				 RequestAttribute.setDescription(request,Description);
				 RequestAttribute.setDate(request,ReserveDate);
				 RequestAttribute.setResult(request, false);
				 request.getRequestDispatcher("/files/meetBooking2.jsp?bookflag=fail").forward(request,response);//成功跳转的页面
			}else if(ms.ReserveRoom(ReserveDate, RoomID, OccupyTime, AttenderNumber, ReserverName, ConnectorName, ConnectMeans, Description, Department,UserID)){
				//System.out.println(ConnectorName+Description);
				 RequestAttribute.setConName(request,ConnectorName);
				 RequestAttribute.setConMeans(request,ConnectMeans);
				 RequestAttribute.setDescription(request,Description);
				 RequestAttribute.setDate(request,ReserveDate);
				 RequestAttribute.setResult(request, true);
				 request.getRequestDispatcher("/files/meetBooking2.jsp?bookflag=success").forward(request,response);//成功跳转的页面
			}else{
				response.sendRedirect("ErrorDeal?error=MD");
				//失败跳转的页面
				}
		}

	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                 this.doGet(request, response);
	}

}
